

import pygame, sys,os
from pygame.locals import *
from helpers import * 

if not pygame.font: print 'Achtung, Fonts ausgeschaltet'
if not pygame.mixer: print 'Achtung, Musik ausgeschaltet'

class Main:

    
    def __init__(self, width=1280,height=720): #h1024

        self.level = 1
        pygame.init()

        self.width = width

        self.height = height

        self.screen = pygame.display.set_mode((self.width, self.height)) 

        self.upKey = False
        
        self.downKey = False


        self.rightKey = False

        self.leftKey = False

        self.blood = False

        self.pustel = True

        self.bloodcounter = 0

        self.points = 0

        self.hero = Hero()
        self.life = self.hero.life
        self.xMove = self.hero.xMove
        self.yMove = self.hero.yMove

        self.controller = 0
        self.checker = False
        self.maxlevel = 2
    def PreLoop(self):
        font = pygame.font.Font(None, 70)
        pygame.key.set_repeat(1, 10)
        self.background = pygame.Surface(self.screen.get_size())
        if(self.level == 1):
            self.background, self.rect = load_image('WallpaperRotGGwT.png')
        elif(self.level !=1):
            self.background, self.rect = load_image('WallpaperRotGG.png')
        starte = True
        
        self.bloodcounter = 0
        self.blood = False
        self.upKey = False
        
        self.downKey = False


        self.rightKey = False

        self.leftKey = False

        while starte:
            for event in pygame.event.get() :
                if event.type == pygame.QUIT: 
                        sys.exit()
                elif event.type == pygame.KEYDOWN :
                    if event.key == K_ESCAPE:
                        sys.exit()        
                    if event.key == K_RETURN:
                        starte = False
            self.screen.blit(self.background, (0, 0)) 
            if(self.level == 1):
                textstart = font.render('Press Enter to Continue',True ,(255,255,0))
                textend = font.render('Press Escape to End',True,(255,255,0))
                self.screen.blit(textstart,(25,500))
                self.screen.blit(textend,(25,550))
            textstart = font.render('World 1 - '+str(self.level),True ,(255,255,0))
            self.screen.blit(textstart,(100,600))
            pygame.display.flip()
            
            
        MainWindow.MainLoop()    

    def WinLoop(self):
        font = pygame.font.Font(None, 70)
        pygame.key.set_repeat(1, 10)
        self.background = pygame.Surface(self.screen.get_size())
        starte = True       
        if self.level == self.maxlevel :        
            self.background, self.rect = load_image('RevengeS.png')
            self.level = 1
            while starte:
                for event in pygame.event.get() :
                    if event.type == pygame.QUIT: 
                        sys.exit()
                    elif event.type == pygame.KEYDOWN:  
                        if event.key == K_ESCAPE:
                            sys.exit()      
                        if event.key == K_RETURN:
                            starte = False
                self.screen.blit(self.background, (0, 0)) 
                textpoints = font.render('Points: '+str(self.points),True,(200,200,0))
                self.screen.blit(textpoints,(200,500))
                pygame.display.flip()
            MainWindow.PreLoop() 

        self.background, self.rect = load_image('lvlClearS.png')
        self.level += 1

            


        self.life = 100 
            
        while starte:
            for event in pygame.event.get() :
                if event.type == pygame.QUIT: 
                        sys.exit()
                elif event.type == pygame.KEYDOWN :
                    if event.key == K_ESCAPE:
                        sys.exit()        
                    if event.key == K_RETURN:
                        starte = False
                self.screen.blit(self.background, (0, 0)) 
                textpoints = font.render('Points: '+str(self.points),True,(200,200,0))
                self.screen.blit(textpoints,(200,600))
                pygame.display.flip()

    def FailLoop(self):
        pygame.key.set_repeat(1, 10)
        self.background = pygame.Surface(self.screen.get_size())
        self.background, self.rect = load_image('GameOverS.png')
        starte = True
        self.points =0
        #self.hero = None
        
        #self.hero = Hero() 
        self.life = 100        
        while starte:
            for event in pygame.event.get() :
                if event.type == pygame.QUIT: 
                        sys.exit()
                elif event.type == pygame.KEYDOWN : 
                    if event.key == K_ESCAPE:
                        sys.exit()       
                    if event.key == K_RETURN:
                        starte = False
                self.screen.blit(self.background, (0, 0)) 
                pygame.display.flip()
        
        
    def MainLoop(self):

        self.LoadBox()
        self.LoadSchluessel()
        self.LoadDoor()
        self.LoadZacken()
        self.LoadDiamant()
        self.LoadHeal()
        self.LoadSprites()
        self.LoadInterface()
        
        font = pygame.font.Font(None, 24)
        
        

        pygame.key.set_repeat(1, 10)
        

        self.background = pygame.Surface(self.screen.get_size())
        #self.background = self.background.convert()
        #self.background.fill((0,0,0))
        if self.level==1: 
            self.background, self.rect = load_image('BSGD1.png')
        else :
            self.background, self.rect = load_image('BSGD2.png')
        
        while 1:
            self.hero.step(self)
            for event in pygame.event.get() :
                if event.type == pygame.QUIT: 
                        sys.exit()
                elif event.type == pygame.KEYDOWN :
                    if event.key == K_ESCAPE:
                        sys.exit()
                    if event.key == pygame.K_UP :
                        self.upKey = True
                    if event.key == pygame.K_DOWN :
                        self.downKey = True
                    if event.key == pygame.K_RIGHT :
                        self.rightKey = True
                    if event.key == pygame.K_LEFT :
                        self.leftKey = True
                elif event.type == pygame.KEYUP :
                    if event.key == pygame.K_UP :
                        self.upKey = False
                    if event.key == pygame.K_DOWN :
                        self.downKey = False
                    if event.key == pygame.K_RIGHT :
                        self.rightKey = False
                    if event.key == pygame.K_LEFT :
                        self.leftKey = False 
                    
            lstCols = False
            self.hero.stand = False
            for box in self.boxes: 
                lstCols1 = pygame.sprite.spritecollide(self.hero,box.sprites, False)
                if lstCols1:
                    
                    if self.hero.rect.y < box.rect.y and self.hero.rect.x+63 != box.rect.x and self.hero.rect.x != box.rect.x+63:
                        self.downKey = False
                    if self.hero.rect.y+63 > box.rect.y+63 and self.hero.rect.x+63 != box.rect.x and self.hero.rect.x != box.rect.x+63:
                        self.upKey = False
                    if self.hero.rect.x < box.rect.x and self.hero.rect.y+63 != box.rect.y and self.hero.rect.y != box.rect.y+63:
                        self.rightKey = False
                    if self.hero.rect.x+63 > box.rect.x+63 and self.hero.rect.y+63 != box.rect.y and self.hero.rect.y != box.rect.y+63:
                        self.leftKey = False

            for schluessel in self.schluessel2: 
                lstCols2 = pygame.sprite.spritecollide(self.hero,schluessel.sprites, False)
                if lstCols2:
                    if self.pustel == True :
                        self.pustel = False
                        schluessel.image = schluessel.image1
                        self.blood = True
                        self.bloodcounter = 1200
                        self.controller += 1
##                        print "Bloodmodus", self.blood, self.bloodcounter
                        self.hero.image1, self.hero.rect1 = load_image('GhostBloodyRight1.png', -1)
                        self.hero.image2, self.hero.rect2 = load_image('GhostBloodyLeft1.png', -1)
                        self.hero.image3, self.hero.rect3 = load_image('GhostBloodyDefault1.png', -1)
                        self.hero.image4, self.hero.rect4 = load_image('GhostBloodyBack1.png', -1)
            tmp = False
            for door in self.doors: 
                lstCols3 = pygame.sprite.spritecollide(self.hero,door.sprites, False)
                if lstCols3:
                    if self.blood == True:
                        door.image = door.image1#
                        self.checker = True
                        MainWindow.WinLoop()
                        tmp = True
                        break
            if tmp:
                break
                        
            for zacken in self.zacken2: 
                lstCols4 = pygame.sprite.spritecollide(self.hero,zacken.sprites, False)
                if lstCols4:
                    self.life -= 5          
##                    print "Leben:", self.life
                    if self.hero.rect.y < zacken.rect.y and self.hero.rect.x+63 != zacken.rect.x and self.hero.rect.x != zacken.rect.x+63:
                        self.hero.rect.y = self.hero.rect.y -8
                    if self.hero.rect.y+63 > zacken.rect.y+63 and self.hero.rect.x+63 != zacken.rect.x and self.hero.rect.x != zacken.rect.x+63:
                        self.hero.rect.y = self.hero.rect.y +8
                    if self.hero.rect.x < zacken.rect.x and self.hero.rect.y+63 != zacken.rect.y and self.hero.rect.y != zacken.rect.y+63:
                        self.hero.rect.x = self.hero.rect.x -8
                    if self.hero.rect.x+63 > zacken.rect.x+63 and self.hero.rect.y+63 != zacken.rect.y and self.hero.rect.y != zacken.rect.y+63:
                        self.hero.rect.x = self.hero.rect.x +8

                        
            for diamant in self.diamanten:
                lstCols5 = pygame.sprite.spritecollide(self.hero,diamant.sprites, True)
                if lstCols5: 
                    self.points += 100
##                    print "Punkte:", self.points            
                    pass
            
            for heal in self.heals:
                lstCols6 = pygame.sprite.spritecollide(self.hero,heal.sprites, True)
                if lstCols6: 
                    self.life = 100
##                    print "Leben:", self.life            
                    pass            
            
            if self.hero.rect.x <= -32 or self.hero.rect.x >= 1312 or self.hero.rect.y >= 1100:
                self.life -= 100 
                
                
            if self.life <= 0:
##                print "Game Over"
                MainWindow.FailLoop()
                break
                    
            if  self.rightKey == True:
                self.hero.move(1)
            if  self.leftKey == True:
                self.hero.move(2)
            if  self.downKey == True:
                self.hero.move(3)
            if  self.upKey == True:
                self.hero.move(4)
                                     
                          
            self.screen.blit(self.background, (0, 0))            
              #mal hero
            for box in self.boxes:               #mal boxes
                box.sprites.draw(self.screen) 
            for schluessel in self.schluessel2:  #mal schluessel
                schluessel.sprites.draw(self.screen) 
            for door in self.doors:              #mal door
                door.sprites.draw(self.screen) 
            for zacken in self.zacken2:          #mal zacken
                zacken.sprites.draw(self.screen)
            for diamant in self.diamanten:       #mal diamant
                diamant.sprites.draw(self.screen)
            for heal in self.heals:              #mal heals
                heal.sprites.draw(self.screen)
            self.hero.sprites.draw(self.screen)
            for interface in self.interfaces:
                interface.sprites.draw(self.screen)  
            
            textlife = font.render(str(self.life),True ,(255,0,255))
            textpoints = font.render(str(self.points),True ,(255,255,0))
            textblood = font.render(str(self.bloodcounter/100),True ,(255,0,0))
            self.screen.blit(textpoints, (300,700))
            self.screen.blit(textlife, (75,700))
            self.screen.blit(textblood, (515,700))
            pygame.display.flip()
            

        MainWindow.PreLoop()
    def LoadSprites(self):
        self.hero = Hero()
        

    def LoadBox(self):
        self.boxes = []
        if self.level == 1:
            self.CreateBoxes(64*0,64*0,64*20,64*1)
            self.CreateBoxes(64*0,64*0,64*1,64*10)
            self.CreateBoxes(64*0,64*10,64*20,64*1)
            self.CreateBoxes(64*19,64*0,64*1,64*10)
            

        if self.level == 2:
            self.CreateBoxes(64*0,64*0,64*20,64*1)
            self.CreateBoxes(64*0,64*0,64*1,64*10)
            self.CreateBoxes(64*0,64*10,64*20,64*1)
            self.CreateBoxes(64*19,64*0,64*1,64*10)
            self.CreateBoxes(64*10,64*1,64*1,64*5)
            self.CreateBoxes(64*11,64*5,64*6,64*1)
        
    def CreateBoxes(self, x, y, width, height):

        for j in range (height/64):
            for i in range( width/64):
                self.boxes.append( Box(x + 64*i,y + 64*j,64,64,j))
                
            
    def LoadSchluessel(self):
        self.schluessel2 = []
        if self.level == 1:    
            self.CreateSchluessel2(64*7,64*2,64,64)
        if self.level == 2:
            self.CreateSchluessel2(64*11,64*1,64,64) 
            
    def CreateSchluessel2(self, x, y, width, height):

        for j in range (height/64):
            for i in range( width/64):
                self.schluessel2.append(Schluessel(x + 64*i,y + 64*j,64,64,j))        
                            
    def LoadDoor(self):

        self.doors = []
        if self.level == 1:    
            self.CreateDoors(64*18,64*5,64,64)
        if self.level == 2:
            self.CreateDoors(64*17.5,64*5,64,64)  
            
    def CreateDoors(self, x, y, width, height):

        for j in range (height/64):
            for i in range( width/64):
                self.doors.append(Door(x + 64*i,y + 64*j,64,64,j))        
    
    def LoadZacken(self):

        self.zacken2 = []
        if self.level == 1:    
            self.CreateZacken2(64*4,64*1,64,64*5)
            self.CreateZacken2(64*8,64*5,64,64*5)
            self.CreateZacken2(64*12,64*1,64,64*4)
            self.CreateZacken2(64*15,64*3,64,64*7)
        if self.level == 2:
            self.CreateZacken2(64*13,64*1.5,64,64)  
            self.CreateZacken2(64*12,64*4,64,64)
            self.CreateZacken2(64*15,64*4,64,64)
            self.CreateZacken2(64*17,64*1.5,64,64)
            self.CreateZacken2(64*8,64*5,64*2,64)
            self.CreateZacken2(64*5,64*8,64,64)
            self.CreateZacken2(64*13,64*6.5,64,64)
            self.CreateZacken2(64*5,64*2,64,64)
            
    def CreateZacken2(self, x, y, width, height):

        for j in range (height/64):
            for i in range( width/64):
                self.zacken2.append(Zacken(x + 64*i,y + 64*j,64,64,j))   
                 
    def LoadDiamant(self):

        self.diamanten = []
        if self.level == 1:    
            self.CreateDiamanten(32*8,32*18,32,32)
            self.CreateDiamanten(32*20,32*4,32,32)
            self.CreateDiamanten(32*34,32*16,32,32)
        if self.level == 2:
            self.CreateDiamanten(64*11.25,64*4.5,32,32) 
            self.CreateDiamanten(64*13.75,64*4.5,32,32)
            self.CreateDiamanten(64*18.5,64*1.25,32,32) 
            
    def CreateDiamanten(self, x, y, width, height):

        for j in range (height/32):
            for i in range( width/32):
                self.diamanten.append(Diamant(x + 32*i,y + 32*j,32,32,j))        
                
    def LoadHeal(self):

        self.heals = []
        if self.level == 1:    
            self.CreateHeals(32*24,32*18,32,32)
        if self.level == 2:
            self.CreateHeals(64*15,64*1.25,32,32)  
            
    def CreateHeals(self, x, y, width, height):

        for j in range (height/32):
            for i in range( width/32):
                self.heals.append(Heal(x + 32*i,y + 32*j,32,32,j))
                
    def LoadInterface(self):
        self.interfaces = []
        self.CreateInterfaces()
        
    def CreateInterfaces(self):
        self.interfaces.append(Interface())    
                    
class Interface(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)  
        self.image, self.rect = load_image('Taskbar1.png') 
        self.rect.move_ip(0,688)   
        self.sprites = pygame.sprite.RenderPlain((self))  
        
class Hero(pygame.sprite.Sprite):

    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('GhostDefault1.png', -1)
        self.image1, self.rect1 = load_image('GhostRight1.png', -1)
        self.image2, self.rect2 = load_image('GhostLeft1.png', -1)
        self.image3, self.rect3 = load_image('GhostDefault1.png', -1)
        self.image4, self.rect4 = load_image('GhostBack1.png', -1)
        self.image5, self.rect5 = load_image('GhostBloodyRight1.png', -1)
        self.image6, self.rect6 = load_image('GhostBloodyLeft1.png', -1)
        self.image7, self.rect7 = load_image('GhostBloodyDefault1.png', -1)
        self.image8, self.rect8 = load_image('GhostBloodyBack1.png', -1)
        self.sprites = pygame.sprite.RenderPlain((self))
        self.x_dist = 1
        self.y_dist = 1 
        self.xMove = 70
        self.yMove = 70
        self.stand = False
        self.jumpSpeed = 0
        self.life = 100   
        
    def pustelreset (self, main):
        if (main.blood == True):
            main.bloodcounter -= 1
##            print main.bloodcounter  
        if (main.bloodcounter == 0 and main.controller > 0):
            if(main.blood == True):
                main.schluessel2[0].image = main.schluessel2[0].image2
                self.image1, self.rect1 = load_image('GhostRight1.png', -1)
                self.image2, self.rect2 = load_image('GhostLeft1.png', -1)
                self.image3, self.rect3 = load_image('GhostDefault1.png', -1)
                self.image4, self.rect4 = load_image('GhostBack1.png', -1)
            main.blood = False
            main.pustel = True


    def move(self, key):
        
        if (key == 1):
            self.xMove = self.x_dist
            self.image = self.image1
        elif (key == 2):
            self.xMove = -self.x_dist
            self.image = self.image2
        if (key == 3):
            self.yMove = self.y_dist
            self.image = self.image3
        elif (key == 4):
            self.yMove = -self.y_dist
            self.image = self.image4            

            
    def step(self, main):

        self.rect = self.rect.move(self.xMove,self.yMove)
        self.xMove = 0
        self.yMove = 0
        self.pustelreset(main)

                   
            
class Box(pygame.sprite.Sprite):
    
    def __init__(self, x, y, width, height, j):
##        print "box at" , x, y, width, height
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('Blockade1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))
        self.stand = j
        
class Schluessel(pygame.sprite.Sprite):
            
    def __init__(self, x, y, width, height, j):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('Blutpustel1.png',-1)
        self.image1, self.rect1 = load_image('BlutpustelDead1.png',-1)
        self.image2, self.rect2 = load_image('Blutpustel1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))        
        self.stand = j
        


                   
class Door(pygame.sprite.Sprite):
            
    def __init__(self, x, y, width, height, j):     
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('TVButton1.png',-1)
        self.image1, self.rect1 = load_image('TVButton1active.png',-1)
        self.image2, self.rect2 = load_image('TVButton1done.png',-1)
        self.image3, self.rect3 = load_image('TVButton1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))        
        self.stand = j
        
class Zacken(pygame.sprite.Sprite):
            
    def __init__(self, x, y, width, height, j):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('Spikes1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))        
        self.stand = j
        
class Diamant(pygame.sprite.Sprite):
            
    def __init__(self, x, y, width, height, j):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('SpiritOrb1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))        
        self.stand = j
        
class Heal(pygame.sprite.Sprite):
            
    def __init__(self, x, y, width, height, j):
        pygame.sprite.Sprite.__init__(self) 
        self.image, self.rect = load_image('LifeOrb1.png',-1)
        self.rect.move_ip(x,y)
        self.sprites = pygame.sprite.RenderPlain((self))        
        self.stand = j

           
if __name__ == "__main__":
    MainWindow = Main()
    MainWindow.PreLoop()
       